/*** Class Name: ifulfillTaskHistoryRestAPIBatch 
* Author: Accenture      
* Requirement/Project Name: iFULFILL
* Requirement/Project Description:SFDC should verify that the CSS system is not down prior to sending an TaskHistory outbound message.
* This batch will process all the Failed taskHistory records where status code not equal to '0 .
* Failed records are send to SIL and update the records based upon the response.
* **/

global class ifulfillTaskHistoryRestAPIBatch implements Database.Batchable<sObject>,Database.AllowsCallouts,Database.Stateful{

    public String query;
    public static List<sObject> objectList;
    static AppEndPoints__c appEndPoints;
    
    global database.querylocator start(Database.BatchableContext BC){
       return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<sObject> scope){    
        objectList = new List<sObject>();
        objectList =scope;   
        connectToSil();
    }    
    /*
    *This method will connect to SIL and process the failed records and update the status based up on the response from SIL.
    */
    static void connectToSil(){    
      String lastModifiedDate;
      String endPointURL;
      String bodyData;
      String []responseArray;
      String autoNotes='';
      List<Fulfillment_Step_History__c> restAPIReponseTaskHistoryList= new List<Fulfillment_Step_History__c>();
      try{
         appEndPoints = AppEndPoints__c.getInstance();
         System.debug('objectList---->'+objectList); 
        for(Sobject obj:objectList){
            Fulfillment_Step_History__c FSH = (Fulfillment_Step_History__c)obj;  
            System.debug('FSH --->'+FSH);           
            lastModifiedDate= FSH.LastModifiedDate.format('MM/dd/yyyy');
            System.debug('lastModified Date'+lastModifiedDate+'appEndpoint--->'+appEndPoints.workflowCompleteURL__c);            
            if(FSH.AutoNotes__c !=null){
              autoNotes=FSH.AutoNotes__c + '';
            }
            endPointURL=appEndPoints.workflowCompleteURL__c+'/UpdateIRFulfillmentTaskHistory?AutoNotes='+EncodingUtil.urlEncode(autoNotes,'UTF-8')+'&CreatedById='+FSH.CreatedByID+'&FullfilmentItem='+FSH.Fullfilment_Item__c+'&Id='+FSH.ID+'&LastModifiedDate='+lastModifiedDate;                  
            bodyData='AutoNotes='+EncodingUtil.urlEncode(autoNotes,'UTF-8')+'&CreatedById='+FSH.CreatedByID+'&FullfilmentItem='+FSH.Fullfilment_Item__c+'&Id='+FSH.ID+'&LastModifiedDate='+lastModifiedDate;
            responseArray =ifulfillWebService.invokeRestAPICall(endPointURL,'TaskHistory',bodyData); 
            System.debug('responseArray --->'+responseArray);
            if(responseArray[0] !=null && responseArray[0].equals('0')){ 
                FSH.Delivery_Status__c=IfullfillTextConstant.FI_PR_DELIVERY_SUCCESS_STATUS;
                FSH.Status_Code__c=responseArray[0];
                restAPIReponseTaskHistoryList.add(FSH);                            
            }else{
                 FSH.Delivery_Status__c=IfullfillTextConstant.FI_PR_DELIVERY_FAILED_STATUS;
                 FSH.Status_Code__c=responseArray[0];
                 FSH.Error_Message__c=responseArray[1];
                 FSH.Error_Stack__c=responseArray[2];
                 restAPIReponseTaskHistoryList.add(FSH);                    
            }            
        } 
        System.debug('restAPIReponseTaskHistoryList--->'+restAPIReponseTaskHistoryList);
        if(restAPIReponseTaskHistoryList.size()>0){
            update restAPIReponseTaskHistoryList;
        }
       }Catch(Exception e){
          System.debug('Excetpion is ifulfillTaskHistoryRestAPIBatch '+e); 
           
       }  
    
    }
    
    global void finish(Database.BatchableContext BC){
       
    }

}